//
//  MainTabBarController.swift
//  Sina
//
//  Created by 沈方武 on 15/9/27.
//  Copyright © 2015年 沈方武. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置tabBar的tintColor
        tabBar.tintColor = UIColor.orangeColor()
        
        // 添加子控制器
        addChildVc()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 添加发布按钮
        tabBar.addSubview(publishBtn)
        
        //计算tabBar按钮的宽度
        let width = UIScreen.mainScreen().bounds.size.width / CGFloat(childViewControllers.count)
        
        // 设置发布按钮的frame
        publishBtn.frame = CGRect(x: 2 * width, y: 0, width: width, height: 49)
    }
    
    // MARK: - 添加所有的子控制器
    private func addChildVc() {
    
        do{
            // 获取plist文件路劲
            let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
            // 获取数据
            let data = NSData(contentsOfFile: path)
            
            // 解析JSON
            let array = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
        
            // 遍历字典
            for dict in array as! [[String : AnyObject]]
            {
                 addChildViewController(dict["vcName"] as! String, imageName: dict["imageName"] as! String , title: dict["title"] as! String)
            }
        }
        catch{
            
            // 主页
            addChildViewController("HomeTableViewController", imageName: "tabbar_home", title: "首页")
            // 消息
            addChildViewController("DiscoverTableViewController", imageName: "tabbar_message_center", title: "消息")
            // 发布
            addChildViewController("PublishViewController", imageName: "", title: "")
            // 发现
            addChildViewController("MessageTableViewController", imageName: "tabbar_discover", title: "广场")
            // 我
            addChildViewController("ProfileTableViewController", imageName: "tabbar_profile", title: "我")
        }
    }
    
    // MARK: - 把子控制器都包装成导航控制器
    private func addChildViewController(VCName: String, imageName:String, title:String) {
         // 动态获取命名空间
        let nsp = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        // 拼接字符串
        let str = "\(nsp).\(VCName)"
        
        // 根据字符串创建对象
        let cls: AnyClass = NSClassFromString(str)!
        
        let childController = (cls as! UIViewController.Type).init()
        
        if imageName != ""
        {
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        }
        
        // 包装成导航控制器
        let nav = UINavigationController()
        nav.addChildViewController(childController)
        
        // 添加到tabBar控制器
        addChildViewController(nav)
        
    }
    
    // MARK: - 懒加载发布按钮
    private lazy var publishBtn:UIButton = {
        
        // 创建按钮
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        
        // 监听按钮点击
        btn.addTarget(self, action: Selector("publishBtnClick:"), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()

    // MARK: - 监听按钮的点击(不能是private)
    func publishBtnClick(button:UIButton) {
    
        SFWLog("点击了发布按钮")
    }

}

